package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IClientCaseService;
import com.mdd.admin.validate.ClientCaseCreateValidate;
import com.mdd.admin.validate.ClientCaseUpdateValidate;
import com.mdd.admin.validate.ClientCaseSearchValidate;
import com.mdd.admin.vo.ClientCaseListedVo;
import com.mdd.admin.vo.ClientCaseDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.ClientCase;
import com.mdd.common.mapper.ClientCaseMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 客户案件实现类
 * @author LikeAdmin
 */
@Service
public class ClientCaseServiceImpl implements IClientCaseService {
        
    @Resource
    ClientCaseMapper clientCaseMapper;

    /**
     * 客户案件列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<ClientCaseListedVo>
     */
    @Override
    public PageResult<ClientCaseListedVo> list(PageValidate pageValidate, ClientCaseSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<ClientCase> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        clientCaseMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:publicationNumber@publication_number:str",
            "=:contractNo@contract_no:str",
            "=:customerDocumentNumber@customer_document_number:str",
            "=:applicant:str",
            "=:ourDocumentNumber@our_document_number:str",
            "=:applicationNumber@application_number:str",
            "like:caseName@case_name:str",
            "=:caseType@case_type:str",
            "between:openingDate@opening_date:str",
        });

        IPage<ClientCase> iPage = clientCaseMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<ClientCaseListedVo> list = new LinkedList<>();
        for(ClientCase item : iPage.getRecords()) {
            ClientCaseListedVo vo = new ClientCaseListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 客户案件详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return ClientCase
     */
    @Override
    public ClientCaseDetailVo detail(Integer id) {
        ClientCase model = clientCaseMapper.selectOne(
                new QueryWrapper<ClientCase>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        ClientCaseDetailVo vo = new ClientCaseDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 客户案件新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(ClientCaseCreateValidate createValidate) {
        ClientCase model = new ClientCase();
        model.setPublicationNumber(createValidate.getPublicationNumber());
        model.setContractNo(createValidate.getContractNo());
        model.setContractName(createValidate.getContractName());
        model.setCustomerDocumentNumber(createValidate.getCustomerDocumentNumber());
        model.setApplicantId(createValidate.getApplicantId());
        model.setApplicant(createValidate.getApplicant());
        model.setOurDocumentNumber(createValidate.getOurDocumentNumber());
        model.setApplicationNumber(createValidate.getApplicationNumber());
        model.setApplicationDate(createValidate.getApplicationDate());
        model.setCaseName(createValidate.getCaseName());
        model.setCaseTypeId(createValidate.getCaseTypeId());
        model.setCaseType(createValidate.getCaseType());
        model.setCaseStateId(createValidate.getCaseStateId());
        model.setCaseState(createValidate.getCaseState());
        model.setBusinessTypeId(createValidate.getBusinessTypeId());
        model.setBusinessType(createValidate.getBusinessType());
        model.setApplicationTypeId(createValidate.getApplicationTypeId());
        model.setApplicationType(createValidate.getApplicationType());
        model.setDesignatedCountry(createValidate.getDesignatedCountry());
        model.setFirstContact(createValidate.getFirstContact());
        model.setOpeningDate(createValidate.getOpeningDate());
        model.setBusinessPersonnelId(createValidate.getBusinessPersonnelId());
        model.setBusinessPersonnel(createValidate.getBusinessPersonnel());
        model.setOriginalSalesmanId(createValidate.getOriginalSalesmanId());
        model.setOriginalSalesman(createValidate.getOriginalSalesman());
        model.setUndertakeDepartmentId(createValidate.getUndertakeDepartmentId());
        model.setUndertakeDepartment(createValidate.getUndertakeDepartment());
        model.setProcessPersonnelId(createValidate.getProcessPersonnelId());
        model.setProcessPersonnel(createValidate.getProcessPersonnel());
        clientCaseMapper.insert(model);
    }

    /**
     * 客户案件编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(ClientCaseUpdateValidate updateValidate) {
        ClientCase model = clientCaseMapper.selectOne(
                new QueryWrapper<ClientCase>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setPublicationNumber(updateValidate.getPublicationNumber());
        model.setContractNo(updateValidate.getContractNo());
        model.setContractName(updateValidate.getContractName());
        model.setCustomerDocumentNumber(updateValidate.getCustomerDocumentNumber());
        model.setApplicantId(updateValidate.getApplicantId());
        model.setApplicant(updateValidate.getApplicant());
        model.setOurDocumentNumber(updateValidate.getOurDocumentNumber());
        model.setApplicationNumber(updateValidate.getApplicationNumber());
        model.setApplicationDate(updateValidate.getApplicationDate());
        model.setCaseName(updateValidate.getCaseName());
        model.setCaseTypeId(updateValidate.getCaseTypeId());
        model.setCaseType(updateValidate.getCaseType());
        model.setCaseStateId(updateValidate.getCaseStateId());
        model.setCaseState(updateValidate.getCaseState());
        model.setBusinessTypeId(updateValidate.getBusinessTypeId());
        model.setBusinessType(updateValidate.getBusinessType());
        model.setApplicationTypeId(updateValidate.getApplicationTypeId());
        model.setApplicationType(updateValidate.getApplicationType());
        model.setDesignatedCountry(updateValidate.getDesignatedCountry());
        model.setFirstContact(updateValidate.getFirstContact());
        model.setOpeningDate(updateValidate.getOpeningDate());
        model.setBusinessPersonnelId(updateValidate.getBusinessPersonnelId());
        model.setBusinessPersonnel(updateValidate.getBusinessPersonnel());
        model.setOriginalSalesmanId(updateValidate.getOriginalSalesmanId());
        model.setOriginalSalesman(updateValidate.getOriginalSalesman());
        model.setUndertakeDepartmentId(updateValidate.getUndertakeDepartmentId());
        model.setUndertakeDepartment(updateValidate.getUndertakeDepartment());
        model.setProcessPersonnelId(updateValidate.getProcessPersonnelId());
        model.setProcessPersonnel(updateValidate.getProcessPersonnel());
        clientCaseMapper.updateById(model);
    }

    /**
     * 客户案件删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        ClientCase model = clientCaseMapper.selectOne(
                new QueryWrapper<ClientCase>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(String.valueOf(System.currentTimeMillis() / 1000));
        clientCaseMapper.updateById(model);
    }

}
